﻿Imports System.Windows
Imports Mystery

Public Class ContentTypeButtonAttribute
    Inherits MysteryDefaultClassAttribute

    Public Property implementing_type As Type
    Public Property order As Integer = -1000
    Public Property should_be_main As Boolean = True
    Public Property normal_icon As String = "Images/object.png"

    Private generic As Type = GetType(ContentTypeButton(Of ))

    Public Overrides Sub setUp()
        If implementing_type IsNot Nothing Then
            If implementing_type.GetInterface(GetType(IButton).FullName) Is Nothing Then
                Throw New Exception(_implementing_type.FullName & " must implement " & GetType(IButton).FullName)
            End If
        Else
            implementing_type = generic.MakeGenericType(used_in)
        End If
    End Sub
End Class

Public Class ContentTypeButton(Of T As IContent)
    Implements IButton

    Public Event Clicked() Implements IButton.Clicked
    Private _text As String
    Private _image_uri As Uri


    Public Sub New()
        Dim ct As ContentType = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentType, T)()
        _text = ct.list_label
        Dim cb As ContentTypeButtonAttribute = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentTypeButtonAttribute, T)()
        Try
            _image_uri = New Uri(cb.normal_icon, UriKind.RelativeOrAbsolute)
        Catch ex As Exception
            _image_uri = New Uri("Images/object.png", UriKind.RelativeOrAbsolute)
        End Try


    End Sub

    Public Function getImage() As System.Windows.Controls.Image Implements IButton.getImage
        Dim result As New Image()
        result.Source = New BitmapImage(_image_uri)
        Return result
    End Function

    Public Function getText() As String Implements IButton.getText
        Return _text
    End Function

    Public Sub Press() Implements IButton.Press
        getGlobalObject(Of InterfaceStatus).showType(GetType(T), force_reload:=False)
        RaiseEvent Clicked()
    End Sub
End Class
